package com.zjuee.web.tporder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.service.TransOrderSolidService;
import com.zjuee.base.dto.common.tporder.TransOrderSolidDto;
import com.zjuee.base.model.common.tporder.WkTransOrderSolidCompanyEntity;
import com.zjuee.base.model.common.tporder.WkTransOrderSolidEntity;
import com.zjuee.base.model.common.tporder.WkTransOrderSolidMaterialEntity;
import com.zjuee.vo.WkTransOrderSolidCompanyVo;
import com.zjuee.vo.WkTransOrderSolidMaterialVo;
import com.zjuee.vo.WkTransOrderSolidVo;
import com.zjuee.web.tporder.mapper.TransOrderSolidCompanyMapper;
import com.zjuee.web.tporder.mapper.TransOrderSolidMapper;
import com.zjuee.web.tporder.mapper.TransOrderSolidMaterialMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TransOrderSolidImpl implements TransOrderSolidService {
    @Resource
    private TransOrderSolidMapper transOrderSolidMapper;
    @Resource
    private TransOrderSolidCompanyMapper transOrderSolidCompanyMapper;

    @Resource
    private TransOrderSolidMaterialMapper transOrderSolidMaterialMapper;
    @Override
    public Long add(WkTransOrderSolidVo type) {
        WkTransOrderSolidEntity wkTransOrderSolidEntity = new WkTransOrderSolidEntity();
        BeanUtils.copyProperties(type, wkTransOrderSolidEntity);
        transOrderSolidMapper.insert(wkTransOrderSolidEntity);
        List<WkTransOrderSolidCompanyVo> wkTransOrderSolidCompanyVos  = type.getWkTransOrderSolidCompanyVos();
        for(WkTransOrderSolidCompanyVo wkTransOrderSolidCompanyVo : wkTransOrderSolidCompanyVos){
            WkTransOrderSolidCompanyEntity wkTransOrderSolidCompanyEntity = new WkTransOrderSolidCompanyEntity();
            BeanUtils.copyProperties(wkTransOrderSolidCompanyVo, wkTransOrderSolidCompanyEntity);
            wkTransOrderSolidCompanyEntity.setWtosId(wkTransOrderSolidEntity.getId());
            transOrderSolidCompanyMapper.insert(wkTransOrderSolidCompanyEntity);
        }

        List<WkTransOrderSolidMaterialVo> wkTransOrderSolidMaterialVos = type.getWkTransOrderSolidMaterialVos();
        for(WkTransOrderSolidMaterialVo WkTransOrderSolidMaterialVo : wkTransOrderSolidMaterialVos){
            WkTransOrderSolidMaterialEntity wkTransOrderSolidMaterialEntity = new WkTransOrderSolidMaterialEntity();
            BeanUtils.copyProperties(WkTransOrderSolidMaterialVo, wkTransOrderSolidMaterialEntity);
            wkTransOrderSolidMaterialEntity.setWtosId(wkTransOrderSolidEntity.getId());
            transOrderSolidMaterialMapper.insert(wkTransOrderSolidMaterialEntity);
        }
        return wkTransOrderSolidEntity.getId();
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        return false;
    }

    @Override
    public boolean update(WkTransOrderSolidVo type) {
        UpdateWrapper<WkTransOrderSolidEntity> updateWrapper=new UpdateWrapper<>();
        WkTransOrderSolidEntity wkTransOrderSolidEntity = new WkTransOrderSolidEntity();

        if("1".equals(type.getWtosmType())){
            wkTransOrderSolidEntity.setWtosRecordApply(type.getWtosRecordApply());
        }
        if("2".equals(type.getWtosmType())){
            wkTransOrderSolidEntity.setWtosStatus(type.getWtosStatus());
        }
        updateWrapper.eq("id",type.getId());
        int affectRows = transOrderSolidMapper.update(wkTransOrderSolidEntity,updateWrapper);
        return affectRows > 0;
    }

    @Override
    public WkTransOrderSolidVo selectById(Long id, Long validateId) {
        return null;
    }

    @Override
    public Page<WkTransOrderSolidVo> list(TransOrderSolidDto dto) {
        return transOrderSolidMapper.getTransOrderSolidList(dto.getPage().convert(),dto);
    }

    @Override
    public LambdaQueryWrapper<WkTransOrderSolidVo> fuzzyQuery(TransOrderSolidDto dto) {
        return null;
    }

    @Override
    public WkTransOrderSolidVo getTransOrderSolid(Long id) {
        WkTransOrderSolidEntity wkTransOrderSolidEntity = transOrderSolidMapper.selectById(id);

        QueryWrapper<WkTransOrderSolidCompanyEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("wtos_id", id);
        List<WkTransOrderSolidCompanyEntity> wkTransOrderSolidCompanyEntitys = transOrderSolidCompanyMapper.selectList(queryWrapper1);

        QueryWrapper<WkTransOrderSolidMaterialEntity> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("wtos_id", id);
        List<WkTransOrderSolidMaterialEntity> wkTransOrderSolidMaterialEntitys = transOrderSolidMaterialMapper.selectList(queryWrapper2);


        WkTransOrderSolidVo wkTransOrderSolidVo =  new WkTransOrderSolidVo();

        List<WkTransOrderSolidCompanyVo> wkTransOrderSolidCompanyVos = new ArrayList();

        List<WkTransOrderSolidMaterialVo> wkTransOrderSolidMaterialVos = new ArrayList();

        BeanUtils.copyProperties(wkTransOrderSolidEntity, wkTransOrderSolidVo);
        for(WkTransOrderSolidCompanyEntity wkTransOrderSolidCompanyEntity : wkTransOrderSolidCompanyEntitys){
            WkTransOrderSolidCompanyVo wkTransOrderSolidCompanyVo = new WkTransOrderSolidCompanyVo();
            BeanUtils.copyProperties(wkTransOrderSolidCompanyEntity, wkTransOrderSolidCompanyVo);
            wkTransOrderSolidCompanyVos.add(wkTransOrderSolidCompanyVo);
        }
        for(WkTransOrderSolidMaterialEntity wkTransOrderSolidMaterialEntity : wkTransOrderSolidMaterialEntitys){
            WkTransOrderSolidMaterialVo wkTransOrderSolidMaterialVo = new WkTransOrderSolidMaterialVo();
            BeanUtils.copyProperties(wkTransOrderSolidMaterialEntity, wkTransOrderSolidMaterialVo);
            wkTransOrderSolidMaterialVos.add(wkTransOrderSolidMaterialVo);
        }


        wkTransOrderSolidVo.setWkTransOrderSolidCompanyVos(wkTransOrderSolidCompanyVos);
        wkTransOrderSolidVo.setWkTransOrderSolidMaterialVos(wkTransOrderSolidMaterialVos);

        return wkTransOrderSolidVo;
    }
}
